package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsComment;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCommentAddCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCommentCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCommentQueryCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsCommentVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersDetailsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.teacher.admin.service.questions.QuestionsCommentService;
import com.bei_en.teacher.admin.service.users.UsersDetailsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.bei_en.common.web.MySessionContext;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:28:27
 */
@Controller
@RequestMapping("/api/Questions")
public class QuestionsCommentAPPController extends BaseController {
	private static String bucketName = "jiaoshi";
	private static String img_endpoint = "oss-cn-hangzhou.aliyuncs.com";// 图片服务
	@Autowired
	QuestionsCommentService questionsCommentService;
	@Autowired
	UsersDetailsService usersDetailsService;

	@RequestMapping("/questionsComment")
	@ResponseBody
	public JsonResult questionsComment(Model model,
			@Valid QuestionsCommentQueryCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {

			List<QuestionsCommentVo> list = new ArrayList<QuestionsCommentVo>();
			list = questionsCommentService.findQuestionsCommentPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			QuestionsCommentVo questionsCommentVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				questionsCommentVo = list.get(i);
				UsersDetailsVo usersDetailsVo = usersDetailsService
						.findById(questionsCommentVo.getCommentPeople());
				if (usersDetailsVo != null) {
					if(usersDetailsVo.getRealName()!=null){
						object.put("user_name", usersDetailsVo.getRealName());
					}else{
						object.put("user_name", "易考用户");
					}
					if(usersDetailsVo.getFacePath()!=null){
						object.put("user_facepath", "http://" + bucketName + "." + img_endpoint
								+ "/" +usersDetailsVo.getFacePath());
					}else{
						object.put("user_facepath", "");
					}
				
				} else {
					object.put("user_name", "非法用户");
					object.put("user_facepath", "");
				}
				object.put("questionsId", questionsCommentVo.getQuestionsId());
				object.put("content", questionsCommentVo.getContent());
				object.put("createTime", questionsCommentVo.getCreateTime()
						.getTime());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsCommentAdd")
	@ResponseBody
	@OAuthRequired
	public JsonResult questionsCommentAdd(Model model,
			@Valid QuestionsCommentAddCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			QuestionsCommentVo questionsComment = new QuestionsCommentVo();
			questionsComment.setCommentPeople(vo.getCommentPeople());
			questionsComment.setContent(vo.getContent());
			questionsComment.setQuestionsId(vo.getQuestionsId());
			questionsComment.setCreateTime(new Date());
			questionsCommentService.insert(questionsComment);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsCommentEdit")
	public JsonResult questionsCommentEdit(Model model,
			@Valid QuestionsCommentCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			QuestionsCommentVo questionsComment = new QuestionsCommentVo();

			questionsCommentService.updateSelective(questionsComment);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
